/**
 * @param {Egg.Application} app - egg application
 */
module.exports = (app) => {
  const { router, controller } = app;
  router.get("/", controller.home.index);
  // 图片上传接口
  router.post("/api/upload/image", controller.upload.image);
  router.post("/api/upload/editorImage", controller.upload.editorImage);
  // 可选：添加删除接口
  router.delete("/api/upload/image/:filename", controller.upload.delete);
  // 用户登录接口
  // 使用 POST 方法，路由为 /api/login
  router.post("login", "/api/login", controller.users.login);
  router.get("login", "/api/logout", controller.users.logout);

  router.resources("users", "/api/users", controller.users);

  // 轮播图管理
  router.resources("banners", "/api/banners", controller.banners);
  router.delete(
    "/api/banners/batchDelete/:ids",
    controller.banners.batchDelete
  );

  // 字典管理
  router.resources("dict", "/api/dict", controller.dict);
  router.get(
    "dict",
    "/api/dictItemsByDictCode",
    controller.dict.dictItemsByDictCode
  );
  router.resources("dictItem", "/api/dictItem", controller.dictItem);

  // 分类路由
  router.get("/api/categories", controller.category.index);
  router.get("/api/categories/tree", controller.category.tree);
  router.get("/api/categories/:id", controller.category.show);
  router.post("/api/categories", controller.category.create);
  router.put("/api/categories/:id", controller.category.update);
  router.delete("/api/categories/:id", controller.category.destroy);

  // 品牌路由
  router.get("/api/brands", controller.brand.index);
  router.get("/api/brands/:id", controller.brand.show);
  router.post("/api/brands", controller.brand.create);
  router.put("/api/brands/:id", controller.brand.update);
  router.delete("/api/brands/:id", controller.brand.destroy);

  // 商品路由
  router.get("/api/products", controller.product.index);
  router.get("/api/products/:id", controller.product.getDetail);
  router.post("/api/products", controller.product.create);
  router.put("/api/products/:id", controller.product.update);
  router.delete("/api/products/:id", controller.product.destroy);
  router.put("/api/products/:id/stock", controller.product.updateStock);

  // 商品图片路由
  router.get("/api/product-images", controller.productImage.index);
  router.post("/api/product-images", controller.productImage.create);
  router.put("/api/product-images/:id", controller.productImage.update);
  router.delete("/api/product-images/:id", controller.productImage.destroy);

  // 商品规格路由
  router.get("/api/product-specs", controller.productSpec.index);
  router.post("/api/product-specs", controller.productSpec.create);
  router.put("/api/product-specs/:id", controller.productSpec.update);
  router.delete("/api/product-specs/:id", controller.productSpec.destroy);

  // 定义一个路由来处理不需要验证的静态资源
  router.get("/public/uploads/*", (ctx) => {
    const path = ctx.params[0]; // 获取请求路径中的文件名或路径
    const filePath = path.join(app.config.baseDir, "app/public", path); // 构造文件路径
    ctx.set("Content-Type", "application/octet-stream"); // 设置内容类型为二进制流
    ctx.body = fs.createReadStream(filePath); // 使用流式读取文件内容并返回
  });
};
